package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.net.Uri
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.api.bean.Norification
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Response

class TestViewModel(app: Application) : MiddleVm(app) {

    val syInit = MutableLiveData<Boolean>(false)
    val syIsRef = MutableLiveData<Boolean>(false)
    val searchTxt = MutableLiveData<String>("")
    val cateId = MutableLiveData<String>("")
    val startDate = MutableLiveData<String>("")
    val endDate = MutableLiveData<String>("")
    val isCateLoading = MutableLiveData<Boolean>(false)
    val isShowing = MutableLiveData<Boolean>(false)
    val isSign = MutableLiveData<Boolean>(false)
    val isShareLoading = MutableLiveData<Boolean>(false)

    fun homeOnNoti() {
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<Norification>>? = null
                out_time {
                    val ret = server.get_notification()
                    emm = ret
                }

                emit(emm)
            }.map {
                var emm: Response<JsonResult<GoodsIndex>>? = null
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.goods_index()
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                }
        }
    }

    fun setAvatar(uri: Uri) {
        if (uri == null) {
            toast("请选择图片")
            return
        }
        isShowing.value = true
//        viewModelScope.launch {
//            flow {
//
//                var emm: Response<JsonResult<UploadRet>>? = null
//
//
//                val file = Util.getLuBan(
//                    getApplication(),
//                    userAvatar.value!!
//                )
//                val requestFile: RequestBody = RequestBody.create(
//                    "multipart/form-data".toMediaTypeOrNull(),
//                    file
//                )
//                val body = MultipartBody.Part.createFormData(
//                    "file",
//                    file.name,
//                    requestFile
//                )
//                val descriptionString = "file"
//                val description = RequestBody.create(
//                    "multipart/form-data".toMediaTypeOrNull(),
//                    descriptionString
//                )
//                out_time {
//                    val ret = server.upload(description, body)
//                    emm = ret
//                }
//
//                emit(emm)
//            }.map {
//                var emm: JsonResult<Ret>? = null
//                it?.body()?.let {
//                    if (it.isSuccess) {
//                        out_time {
//                            val ret = server.user_avatar(it.data!!.url)
//                            emm = ret?.body()
//                        }
//                    } else {
//                        emm = JsonResult<Ret>().apply {
//                            message = it.message
//                        }
//                    }
//                }
//                emm
//            }.flowOn(Dispatchers.IO).onCompletion { cause ->
//                isShowing.value = false
//            }.catch { cause ->
//                if (cause != null) {
//                    toast(cause.message)
//                }
//            }.collectLatest {
//                callSetAvatar.value = it
//                it?.let {
//                    if (it.isSuccess) {
//                        toast("设置成功")
//                    } else {
//                        toast(it.message)
//                    }
//                }
//            }
//        }
    }




}